exports = function (model) {
    var self = this;
    var badge = {
        value: 0
    };

    var ismobile = vue.$UserAgent.isMobile;

    var Order = true;

    var home = true;

    var cateringPosition_id = "0";

    var filterMethod = function (val, row, colm) {
        SelectProducts(row, {}, 128);
        return true;
    }

    var setOrder = function (l, FeesPaid) {
        if (FeesPaid == null) {

            FeesPaid = getFeesPaid(l);

            if (FeesPaid == null) {
                Order = true;
                vue.UIEventPool.run("RestaurantOrder_Carousel", "Show", {
                    id: "Main"
                });
                return
            }

            vue.$prompt(vue.$getTitle("@total_cost", FeesPaid + ".000"), vue.$getTitle("@sys_prompt"), {
                customClass: "",
                confirmButtonText: vue.$getTitle('@sys_ok'),
                cancelButtonText: vue.$getTitle('@sys_cancel'),
                inputValue: FeesPaid + "",
                inputType: "number",
                inputPattern: /^[0-9]+(.[0-9]{1,3})?$/,
                inputErrorMessage: '5.555'
            }).then(function (value) {
                setOrder(l, value);
            }).catch(function () {
                setOrder(l);
            });
            return
        }
        if (Order) {
            var self = vue;
            Order = false;
            var obj = {
                jsondb: JSON.stringify(l),
                cateringPosition_id:cateringPosition_id,
                payment:FeesPaid,
                type: "Orders"
            };
            self.$Ajax('{domain}./Products/setOrder/', obj).then(function (response) {
                vue.UIEventPool.run("Orderslist", "Close");
                home = true;
                setvalue(0);
                Order = true;
                vue.UIEventPool.run("RestaurantOrder_Carousel", "Show", {
                    id: "Main"
                });

                addmobilemenu(Tablelist_menu)

            }).catch(function (error) {
                Order = true;
                self.$message(self.$getTitle('下单未成功'));
                console.log(error)
            });
        }
    }
    var setbadge = function (obj, val) {
        val = val || 0;
        obj.badge = obj.badge || 0;
        if (val > 0) {
            obj.badge++;
        } else if (val < 0) {
            obj.badge--;
        }
        if (obj.badge > 0) {
            obj.Badge = {
                type: "danger"
            }
        } else {
            obj.badge = 0;
            obj.Badge = {
                type: "primary"
            }
        }
    }
    var SelectProducts = function (row, obj, off) {

        var Olinst = vue.$ListForm.get(cateringPosition_id);
        var key = "O_" + row.id;
        if (Olinst[key] == null && off == 128) {
            return
        }
        var len = off == 128 ? 0 : 1;

        console.log(obj);

        if (off == 128) {
            row.badge = Olinst[key].len;
        };
        setbadge(row, len);
        if (Olinst[key] == null) {
            Olinst[key] = {
                id: row.id,
                cateringPosition_id: cateringPosition_id,
                title: row.title,
                Price: row.Price,
                row: row,
                len: 0
            }
        } else {
            Olinst[key].row = row;
        }
        Olinst[key].len = row.badge;
        vue.$ListForm.set(Olinst);
        setvalue(cateringPosition_id)
    }
    var getFeesPaid = function (l) {
        var FeesPaid = 0
        for (var i = 0; i < l.length; i++) {
            var element = l[i];
            if (element.id == -1) {
                FeesPaid = element.Price;
            }
        };
        return FeesPaid;
    }
    var setvalue = function (id) {
        var List = [];
        var Olinst = vue.$ListForm.get(id);
        var db = {
            id: -1,
            cateringPosition_id: id,
            title: '_____',
            Price: 0,
            len: 0
        };
        for (var key in Olinst) {
            var cbj = Olinst[key];
            List.push(cbj);
            db.Price = db.Price + (cbj.Price * cbj.len);
            db.len = db.len + cbj.len
        };

        badge.value = db.len;

        List.push(db);

        if (ismobile) {
            vue.UIEventPool.run("mOrderslist", "setDataList", List)
        } else {
            vue.UIEventPool.run("Orderslist", "setDataList", List)
        }

        return List
    }

    var getHome = function (id) {
        id = id || 0;
        vue.$ListForm.get(id);
        setvalue(id);
        return;
    }
    var addProducts = function (row, obj) {

        home = false;
        var key = "O_" + row.id;
        var Olinst = vue.$ListForm.get(cateringPosition_id);
        if (Olinst && Olinst[key] != null) {

            var Taam = Olinst[key];
            var _row = Taam.row;
            if (obj.__id == "minus") {
                Taam.len = Taam.len - 1;
                setbadge(_row, -1);
                if (Taam.len <= 0) {
                    delete Olinst[key];
                } else {
                    Olinst[key] = Taam;
                }
            } else if (obj.__id == "add") {
                setbadge(_row, 1);
                Taam.len = Olinst[key].len + 1;

                Olinst[key] = Taam;
            };
            vue.$ListForm.set(Olinst);
        };

        var l = setvalue(cateringPosition_id);

        if (obj.__id == "ok") {
            var FeesPaid = null;
            var mproducts_menu = json_extract(self, "$.Menu.List.OrderList.Menu.List");
            if (mproducts_menu == null) {
                FeesPaid = getFeesPaid(l);
            }
            setOrder(l, FeesPaid);
        }
    };

    for (var key in model) {

        this[key] = model[key];
    }
    var json_extract = function (json, name) {
        if (name && typeof name == 'string' && name[0] == "$") {
            var val = name.split('.');
            if (val.length == 1) {
                return json;
            }
            var key = val[1];
            val.splice(1, 1);
            var _path = val.join('.');
            if (json[key]) {
                return json_extract(json[key], _path);
            } else if (key.indexOf('[') > 0 && key.indexOf(']') > 0) {
                var val = key.split('[');
                key = val[0];
                var i = val[1];
                i = i.split(']');
                i = i[0];
                i = parseInt(i);
                if (json[key] && json[key].length > i) {
                    var arr = json[key];
                    return json_extract(arr[i], _path);
                } else {
                    return null;
                }
            } else {
                return null;
            }

        }
        return json;
    }

    var Tablelist = self.List.a1.Form.List.main.Form;

    var Tablelist_menu = json_extract(self, "$.Menu.List.cateringPosition.Menu.List.btn.__id");

    var mproducts_menu = json_extract(self, "$.Menu.List.products.Menu.List.btn.__id");

    var Products = {};




    var tabClick = function (val) {
        if (val && val.value == "Orderslist") {
            setvalue(cateringPosition_id);
        } else {
            ///  Products.InitVal++;
        }
    };
    var RightMenu = {
        visible: !0,
        icon: "el-icon-more-outline",
        Click: function (e) {
            if (home) {
                if (Tablelist_menu) {
                    Tablelist.MenuClick();
                }
            } else {
                if (mproducts_menu) {
                    mProducts.MenuClick();
                }
            }
        }
    };

    var addmobilemenu = function (off) {
        if (off) {
            RightMenu.visible = true;
        } else {
            RightMenu.visible = false;
        }
    }

    Tablelist.SelectEvent = function (obj, row) {


        badge.value = 0;

        cateringPosition_id = obj.id;

        // Products.Parameter.data.id = obj.id;

        Products.NotInitialization = true;

        var id = ismobile ? 'm_showcateringPosition' : 'showcateringPosition'
        vue.UIEventPool.run("RestaurantOrder_Carousel", "Show", {
            id: id
        });

        getHome(obj.id);


        home = false;

        if (ismobile) {

            addmobilemenu(mproducts_menu);

            vue.UIEventPool.run("Products_tabs", "Show", {
                id: "products_list"
            });

            Products.InitVal++;


        } else {

            Products.InitVal++;
        }

    };



    if (Tablelist_menu == null) {
        delete self.List.a1.Form.List.drawer
    } else {

        var drawer = self.List.a1.Form.List.drawer;

        drawer.direction = (document.body.dir == 'rtl' ? 'ltr' : 'rtl');

        Tablelist.MenuClick = function () {
            home = true;
            drawer.InfoFormVisible = true;
        };
        drawer.condition.Select = function (obj, key) {
            home = true;
            Tablelist.Parameter.data.cateringPositionType = obj.id;
            Tablelist.InitVal++;
            drawer.InfoFormVisible = false;
        }
    }

    if (ismobile) {

        var mProducts = self.List.showMcateringPosition.Form.List.main.Form.List.a1.Form.List.main.Form;

        Products = mProducts;

        mProducts.List = {};

        mProducts.filterMethod = filterMethod;

        var maside = self.List.showMcateringPosition.Form.List.main.Form.List.a2.Form;


        badge = self.List.showMcateringPosition.Form.List.main.Form.List.a2.badge;

        badge.value = 0;

        var tabs = self.List.showMcateringPosition.Form.List.main.Form;

        tabs.tabClick = tabClick;

        maside.OperationClick = addProducts;

        if (mproducts_menu == null) {
            delete self.List.showMcateringPosition.Form.List.main.Form.List.a1.Form.List.drawer;
        } else {
            var mProductsType = self.List.showMcateringPosition.Form.List.main.Form.List.a1.Form.List.drawer;


            mProductsType.direction = (document.body.dir == 'rtl' ? 'ltr' : 'rtl');
            mProductsType.condition.Select = function (obj) {

                home = false;

                mProductsType.InfoFormVisible = false;

                mProducts.Parameter.data.ProductsType = obj.id;

                mProducts.InitVal++;


            };

            mProducts.MenuClick = function () {
                mProductsType.InfoFormVisible = true;
            }

        }



        mProducts.SelectEvent = SelectProducts;

        vue.$InitAppTopMenu(RightMenu)
        addmobilemenu(Tablelist_menu)
        Tablelist.List = {}
        Tablelist.card = {
            width: 150,
            height: 150
        };
        delete self.List.showcateringPosition;
    } else {


        var obj = json_extract(self, "$.Menu.List.cateringPosition.Menu.List");


        Tablelist.List = obj;

        var pProducts = self.List.showcateringPosition.Form.List.mainForm.List.main.Form;

        pProducts.filterMethod = filterMethod;

        var obj = json_extract(self, "$.Menu.List.products.Menu.List");

        pProducts.List = obj;

        Products = pProducts;

        pProducts.SelectEvent = SelectProducts;

        var aside = self.List.showcateringPosition.Form.List.aside.Form;

        aside.OperationClick = addProducts;


        if (mproducts_menu == null) {
            delete self.List.showcateringPosition.Form.List.mainForm.List.drawer;
        } else {
            var ProductsType = self.List.showcateringPosition.Form.List.mainForm.List.drawer;

            ProductsType.direction = (document.body.dir == 'rtl' ? 'ltr' : 'rtl');

            ProductsType.condition.Select = function (obj) {

                pProducts.Parameter.data.ProductsType = obj.id;

                ProductsType.InfoFormVisible = false;

                pProducts.InitVal++;
            };

            pProducts.MenuClick = function () {
                ProductsType.InfoFormVisible = true;
            };
        }
        delete self.List.showMcateringPosition;

    }
}